package cn.car91.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import cn.car91.R;
import cn.car91.bean.RepairShop;
import cn.car91.config.InitApplication;
import cn.car91.utils.ImageManager;

public class SpreadListAdapter extends BaseAdapter {

    private List<RepairShop> mList;
    private Context mContext;
    private InitApplication initApplication;
    static ImageManager imageManager;

    public SpreadListAdapter(Context pContext) {
        this.mContext = pContext;
        this.mList = new ArrayList<RepairShop>();
        this.initApplication = (InitApplication) pContext.getApplicationContext();
        imageManager = new ImageManager(pContext, "sdcard/car91/temp/repairshop");
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return mList.size();
    }

    @Override
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return mList.get(arg0);
    }

    public String getRepairShopId(int which){
    	return mList.get(which).getId();
    }
    
    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return mList.get(arg0).getRid();
    }

    @Override
    public View getView(int arg0, View arg1, ViewGroup arg2) {
        // TODO Auto-generated method stub
        LayoutInflater layoutInflater = LayoutInflater.from(mContext);
        arg1 = layoutInflater.inflate(R.layout.view_spread_item, null);

        if (arg1 != null) {
        	ImageView logo = (ImageView) arg1.findViewById(R.id.repairshop_logo);
        	TextView name = (TextView) arg1.findViewById(R.id.repairshop_name);
        	TextView address = (TextView) arg1.findViewById(R.id.repairshop_address);
        	ImageView rank = (ImageView) arg1.findViewById(R.id.repairshop_rank);
        	RepairShop repairShop = (RepairShop) getItem(arg0);
        	imageManager.displayImage(initApplication.getCenterServerUrl()+"/"+repairShop.getLogo(), logo, mContext.getResources().getDrawable(R.drawable.load_error));
        	name.setText(repairShop.getName());
        	address.setText(repairShop.getDetail().getAddress());
        	if(repairShop.getDetail().getRank()==null||repairShop.getDetail().getRank()<=0){
        		rank.setVisibility(View.GONE);
        	}else{
        		rank.setVisibility(View.VISIBLE);
        	}
        }

        return arg1;
    }

    public List<RepairShop> getmList() {
        return mList;
    }

    public void setmList(List<RepairShop> mList) {
        this.mList = mList;
    }

}
